using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using Momentum.Static;
using System;
using System.Collections.Generic;
using System.Text;

namespace Momentum.Types
{
    public class mWeapon : mObject
    {

        #region Instance Variables


        protected float m_InitialVelocity;
        protected float m_Delay;
        protected float m_ToNextShot;
        protected List<mBullet> m_Bullets;


        #endregion

        #region Getters and Setters


        public float InitialVelocity
        {
            get { return m_InitialVelocity; }
            set { m_InitialVelocity = value; }
        }

        [ContentSerializerIgnore]
        public List<mBullet> Bullets
        {
            get { return m_Bullets; }
        }

        public float Delay
        {
            get { return m_Delay; }
            set { m_Delay = value; m_ToNextShot = value; }
        }

        [ContentSerializerIgnore]
        public float ToNextShot
        {
            get { return m_ToNextShot; }
            set { m_ToNextShot = value; }
        }


        #endregion

        #region Initialization


        /// <summary>
        /// Default constructor
        /// </summary>
        public mWeapon()
            : base()
        {
            m_Bullets = new List<mBullet>();
        }

        public void Load(ContentManager content)
        {
            m_ObjectTexture = content.Load<Texture2D>("Weapons/" + m_Name + "Texture");
            m_ObjectShadowTexture = content.Load<Texture2D>("Weapons/" + m_Name + "ShadowTexture");
        }


        #endregion

        #region Actions


        public void Shoot(mTank tank)
        {
            m_Bullets.Add(new mBullet(this, tank));
        }

        public void Update()
        {
            foreach (mBullet bullet in m_Bullets)
            {
                bullet.Move(0.0f);
                bullet.UpdatePosition();
            }
        }


        #endregion

        #region Draw


        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach (mBullet bullet in m_Bullets)
            {
                bullet.CameraPosition = m_CameraPosition;
                bullet.SetPositionToCircularWorld();
                bullet.Draw(spriteBatch);
            }
        }


        #endregion

        #region Serialization


        public override void SerializeWH(PacketWriter packetWriter)
        {
            packetWriter.Write(m_Bullets.Count);
            foreach (mBullet bullet in m_Bullets)
            {
                bullet.SerializeWH(packetWriter);
            }
        }

        public override void Deserialize(PacketReader packetReader)
        {
            m_Bullets.Clear();
            int count = packetReader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                mBullet bullet = new mBullet(this);
                bullet.Deserialize(packetReader);
                m_Bullets.Add(bullet);
            }
        }


        #endregion

    }

    #region Content Reader and Writer

    public class mWeaponContentReader : ContentTypeReader<mWeapon>
    {
        protected override mWeapon Read(ContentReader input, mWeapon existingInstance)
        {
            mWeapon weapon = new mWeapon();
            weapon.Name = input.ReadString();
            weapon.CollisionRadius = input.ReadInt32();
            weapon.Mass = input.ReadInt32();
            weapon.ObjectScale = (float)input.ReadSingle();
            weapon.ShadowOffset = input.ReadVector2();
            weapon.TotalHP = input.ReadInt32();
            weapon.InitialVelocity = (float)input.ReadSingle();
            weapon.Delay = (float)input.ReadSingle();
            return weapon;
        }
    }

    [ContentTypeWriter]
    public class mWeaponContentWriter : ContentTypeWriter<mWeapon>
    {
        protected override void Write(ContentWriter output, mWeapon weapon)
        {
            output.Write(weapon.Name);
            output.Write(weapon.CollisionRadius);
            output.Write(weapon.Mass);
            output.Write(weapon.ObjectScale);
            output.Write(weapon.ShadowOffset);
            output.Write(weapon.TotalHP);
            output.Write(weapon.InitialVelocity);
            output.Write(weapon.Delay);
        }
        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(mWeaponContentReader).AssemblyQualifiedName;
        }
    }

    #endregion

}
